﻿using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Fuji
{
	/// <summary>
	/// <b>[商业授权]</b> 富士的SPB虚拟的PLC，线圈支持X,Y,M的读写，其中X只能远程读，寄存器支持D,R,W的读写操作。<br />
	/// <b>[Authorization]</b> Fuji's SPB virtual PLC, the coil supports X, Y, M read and write, 
	/// X can only be read remotely, and the register supports D, R, W read and write operations.
	/// </summary>
	// Token: 0x020000A7 RID: 167
	public class FujiSPBServer : NetworkDataServerBase
	{
		/// <summary>
		/// 实例化一个富士SPB的网口和串口服务器，支持数据读写操作
		/// </summary>
		// Token: 0x06000C69 RID: 3177 RVA: 0x00048E7C File Offset: 0x0004707C
		public FujiSPBServer()
		{
			this.xBuffer = new SoftBuffer(65536);
			this.yBuffer = new SoftBuffer(65536);
			this.mBuffer = new SoftBuffer(65536);
			this.dBuffer = new SoftBuffer(131072);
			this.rBuffer = new SoftBuffer(131072);
			this.wBuffer = new SoftBuffer(131072);
			base.ByteTransform = new RegularByteTransform();
			base.ByteTransform.DataFormat = DataFormat.CDAB;
			base.WordLength = 1;
			this.serialPort = new SerialPort();
		}

		/// <inheritdoc cref="P:HslCommunication.ModBus.ModbusTcpNet.DataFormat" />
		// Token: 0x170001E9 RID: 489
		// (get) Token: 0x06000C6A RID: 3178 RVA: 0x00048F24 File Offset: 0x00047124
		// (set) Token: 0x06000C6B RID: 3179 RVA: 0x00048F41 File Offset: 0x00047141
		public DataFormat DataFormat
		{
			get
			{
				return base.ByteTransform.DataFormat;
			}
			set
			{
				base.ByteTransform.DataFormat = value;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.ModBus.ModbusTcpNet.IsStringReverse" />
		// Token: 0x170001EA RID: 490
		// (get) Token: 0x06000C6C RID: 3180 RVA: 0x00048F54 File Offset: 0x00047154
		// (set) Token: 0x06000C6D RID: 3181 RVA: 0x00048F71 File Offset: 0x00047171
		public bool IsStringReverse
		{
			get
			{
				return base.ByteTransform.IsStringReverseByteWord;
			}
			set
			{
				base.ByteTransform.IsStringReverseByteWord = value;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Station" />
		// Token: 0x170001EB RID: 491
		// (get) Token: 0x06000C6E RID: 3182 RVA: 0x00048F84 File Offset: 0x00047184
		// (set) Token: 0x06000C6F RID: 3183 RVA: 0x00048F9C File Offset: 0x0004719C
		public int Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <inheritdoc />
		// Token: 0x06000C70 RID: 3184 RVA: 0x00048FA8 File Offset: 0x000471A8
		protected override byte[] SaveToBytes()
		{
			byte[] array = new byte[589824];
			this.xBuffer.GetBytes().CopyTo(array, 0);
			this.yBuffer.GetBytes().CopyTo(array, 65536);
			this.mBuffer.GetBytes().CopyTo(array, 131072);
			this.dBuffer.GetBytes().CopyTo(array, 196608);
			this.rBuffer.GetBytes().CopyTo(array, 327680);
			this.wBuffer.GetBytes().CopyTo(array, 458752);
			return array;
		}

		/// <inheritdoc />
		// Token: 0x06000C71 RID: 3185 RVA: 0x0004904C File Offset: 0x0004724C
		protected override void LoadFromBytes(byte[] content)
		{
			bool flag = content.Length < 589824;
			if (flag)
			{
				throw new Exception("File is not correct");
			}
			this.xBuffer.SetBytes(content, 0, 65536);
			this.yBuffer.SetBytes(content, 65536, 65536);
			this.mBuffer.SetBytes(content, 131072, 65536);
			this.dBuffer.SetBytes(content, 196608, 131072);
			this.rBuffer.SetBytes(content, 327680, 131072);
			this.wBuffer.SetBytes(content, 458752, 131072);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000C72 RID: 3186 RVA: 0x000490FC File Offset: 0x000472FC
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = new OperateResult<byte[]>();
			OperateResult<byte[]> result;
			try
			{
				char c = address[0];
				char c2 = c;
				if (c2 <= 'Y')
				{
					if (c2 <= 'M')
					{
						if (c2 == 'D')
						{
							goto IL_109;
						}
						if (c2 != 'M')
						{
							goto IL_175;
						}
						goto IL_E2;
					}
					else
					{
						if (c2 == 'R')
						{
							goto IL_12D;
						}
						switch (c2)
						{
						case 'W':
							goto IL_151;
						case 'X':
							break;
						case 'Y':
							goto IL_BB;
						default:
							goto IL_175;
						}
					}
				}
				else if (c2 <= 'm')
				{
					if (c2 == 'd')
					{
						goto IL_109;
					}
					if (c2 != 'm')
					{
						goto IL_175;
					}
					goto IL_E2;
				}
				else
				{
					if (c2 == 'r')
					{
						goto IL_12D;
					}
					switch (c2)
					{
					case 'w':
						goto IL_151;
					case 'x':
						break;
					case 'y':
						goto IL_BB;
					default:
						goto IL_175;
					}
				}
				return OperateResult.CreateSuccessResult<byte[]>(this.xBuffer.GetBytes(Convert.ToInt32(address.Substring(1)) * 2, (int)(length * 2)));
				IL_BB:
				return OperateResult.CreateSuccessResult<byte[]>(this.yBuffer.GetBytes(Convert.ToInt32(address.Substring(1)) * 2, (int)(length * 2)));
				IL_E2:
				return OperateResult.CreateSuccessResult<byte[]>(this.mBuffer.GetBytes(Convert.ToInt32(address.Substring(1)) * 2, (int)(length * 2)));
				IL_109:
				return OperateResult.CreateSuccessResult<byte[]>(this.dBuffer.GetBytes(Convert.ToInt32(address.Substring(1)) * 2, (int)(length * 2)));
				IL_12D:
				return OperateResult.CreateSuccessResult<byte[]>(this.rBuffer.GetBytes(Convert.ToInt32(address.Substring(1)) * 2, (int)(length * 2)));
				IL_151:
				return OperateResult.CreateSuccessResult<byte[]>(this.wBuffer.GetBytes(Convert.ToInt32(address.Substring(1)) * 2, (int)(length * 2)));
				IL_175:
				throw new Exception(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				operateResult.Message = ex.Message;
				result = operateResult;
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000C73 RID: 3187 RVA: 0x000492C0 File Offset: 0x000474C0
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = new OperateResult<byte[]>();
			OperateResult result;
			try
			{
				char c = address[0];
				char c2 = c;
				if (c2 <= 'Y')
				{
					if (c2 <= 'M')
					{
						if (c2 == 'D')
						{
							goto IL_106;
						}
						if (c2 != 'M')
						{
							goto IL_16F;
						}
						goto IL_E0;
					}
					else
					{
						if (c2 == 'R')
						{
							goto IL_129;
						}
						switch (c2)
						{
						case 'W':
							goto IL_14C;
						case 'X':
							break;
						case 'Y':
							goto IL_BA;
						default:
							goto IL_16F;
						}
					}
				}
				else if (c2 <= 'm')
				{
					if (c2 == 'd')
					{
						goto IL_106;
					}
					if (c2 != 'm')
					{
						goto IL_16F;
					}
					goto IL_E0;
				}
				else
				{
					if (c2 == 'r')
					{
						goto IL_129;
					}
					switch (c2)
					{
					case 'w':
						goto IL_14C;
					case 'x':
						break;
					case 'y':
						goto IL_BA;
					default:
						goto IL_16F;
					}
				}
				this.xBuffer.SetBytes(value, Convert.ToInt32(address.Substring(1)) * 2);
				return OperateResult.CreateSuccessResult();
				IL_BA:
				this.yBuffer.SetBytes(value, Convert.ToInt32(address.Substring(1)) * 2);
				return OperateResult.CreateSuccessResult();
				IL_E0:
				this.mBuffer.SetBytes(value, Convert.ToInt32(address.Substring(1)) * 2);
				return OperateResult.CreateSuccessResult();
				IL_106:
				this.dBuffer.SetBytes(value, Convert.ToInt32(address.Substring(1)) * 2);
				return OperateResult.CreateSuccessResult();
				IL_129:
				this.rBuffer.SetBytes(value, Convert.ToInt32(address.Substring(1)) * 2);
				return OperateResult.CreateSuccessResult();
				IL_14C:
				this.wBuffer.SetBytes(value, Convert.ToInt32(address.Substring(1)) * 2);
				return OperateResult.CreateSuccessResult();
				IL_16F:
				throw new Exception(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				operateResult.Message = ex.Message;
				result = operateResult;
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000C74 RID: 3188 RVA: 0x00049480 File Offset: 0x00047680
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<bool[]> result;
			try
			{
				int num = 0;
				bool flag = address.LastIndexOf('.') > 0;
				if (flag)
				{
					num = HslHelper.GetBitIndexInformation(ref address);
					num = Convert.ToInt32(address.Substring(1)) * 16 + num;
				}
				else
				{
					bool flag2 = address[0] == 'X' || address[0] == 'x' || address[0] == 'Y' || address[0] == 'y' || address[0] == 'M' || address[0] == 'm';
					if (flag2)
					{
						num = Convert.ToInt32(address.Substring(1));
					}
				}
				char c = address[0];
				char c2 = c;
				if (c2 <= 'Y')
				{
					if (c2 <= 'M')
					{
						if (c2 == 'D')
						{
							goto IL_158;
						}
						if (c2 != 'M')
						{
							goto IL_19A;
						}
						goto IL_142;
					}
					else
					{
						if (c2 == 'R')
						{
							goto IL_16E;
						}
						switch (c2)
						{
						case 'W':
							goto IL_184;
						case 'X':
							break;
						case 'Y':
							goto IL_12C;
						default:
							goto IL_19A;
						}
					}
				}
				else if (c2 <= 'm')
				{
					if (c2 == 'd')
					{
						goto IL_158;
					}
					if (c2 != 'm')
					{
						goto IL_19A;
					}
					goto IL_142;
				}
				else
				{
					if (c2 == 'r')
					{
						goto IL_16E;
					}
					switch (c2)
					{
					case 'w':
						goto IL_184;
					case 'x':
						break;
					case 'y':
						goto IL_12C;
					default:
						goto IL_19A;
					}
				}
				return OperateResult.CreateSuccessResult<bool[]>(this.xBuffer.GetBool(num, (int)length));
				IL_12C:
				return OperateResult.CreateSuccessResult<bool[]>(this.yBuffer.GetBool(num, (int)length));
				IL_142:
				return OperateResult.CreateSuccessResult<bool[]>(this.mBuffer.GetBool(num, (int)length));
				IL_158:
				return OperateResult.CreateSuccessResult<bool[]>(this.dBuffer.GetBool(num, (int)length));
				IL_16E:
				return OperateResult.CreateSuccessResult<bool[]>(this.rBuffer.GetBool(num, (int)length));
				IL_184:
				return OperateResult.CreateSuccessResult<bool[]>(this.wBuffer.GetBool(num, (int)length));
				IL_19A:
				throw new Exception(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				result = new OperateResult<bool[]>(ex.Message);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkDeviceBase.Write(System.String,System.Boolean[])" />
		// Token: 0x06000C75 RID: 3189 RVA: 0x00049668 File Offset: 0x00047868
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] value)
		{
			OperateResult result;
			try
			{
				int num = 0;
				bool flag = address.LastIndexOf('.') > 0;
				if (flag)
				{
					HslHelper.GetBitIndexInformation(ref address);
					num = Convert.ToInt32(address.Substring(1)) * 16 + num;
				}
				else
				{
					bool flag2 = address[0] == 'X' || address[0] == 'x' || address[0] == 'Y' || address[0] == 'y' || address[0] == 'M' || address[0] == 'm';
					if (flag2)
					{
						num = Convert.ToInt32(address.Substring(1));
					}
				}
				char c = address[0];
				char c2 = c;
				if (c2 <= 'Y')
				{
					if (c2 <= 'M')
					{
						if (c2 == 'D')
						{
							goto IL_15B;
						}
						if (c2 != 'M')
						{
							goto IL_1A0;
						}
						goto IL_144;
					}
					else
					{
						if (c2 == 'R')
						{
							goto IL_172;
						}
						switch (c2)
						{
						case 'W':
							goto IL_189;
						case 'X':
							break;
						case 'Y':
							goto IL_12D;
						default:
							goto IL_1A0;
						}
					}
				}
				else if (c2 <= 'm')
				{
					if (c2 == 'd')
					{
						goto IL_15B;
					}
					if (c2 != 'm')
					{
						goto IL_1A0;
					}
					goto IL_144;
				}
				else
				{
					if (c2 == 'r')
					{
						goto IL_172;
					}
					switch (c2)
					{
					case 'w':
						goto IL_189;
					case 'x':
						break;
					case 'y':
						goto IL_12D;
					default:
						goto IL_1A0;
					}
				}
				this.xBuffer.SetBool(value, num);
				return OperateResult.CreateSuccessResult();
				IL_12D:
				this.yBuffer.SetBool(value, num);
				return OperateResult.CreateSuccessResult();
				IL_144:
				this.mBuffer.SetBool(value, num);
				return OperateResult.CreateSuccessResult();
				IL_15B:
				this.dBuffer.SetBool(value, num);
				return OperateResult.CreateSuccessResult();
				IL_172:
				this.rBuffer.SetBool(value, num);
				return OperateResult.CreateSuccessResult();
				IL_189:
				this.wBuffer.SetBool(value, num);
				return OperateResult.CreateSuccessResult();
				IL_1A0:
				throw new Exception(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				result = new OperateResult<bool[]>(ex.Message);
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000C76 RID: 3190 RVA: 0x00049858 File Offset: 0x00047A58
		protected override void ThreadPoolLoginAfterClientCheck(Socket socket, IPEndPoint endPoint)
		{
			AppSession appSession = new AppSession();
			appSession.IpEndPoint = endPoint;
			appSession.WorkSocket = socket;
			bool isSuccess = socket.BeginReceiveResult(new AsyncCallback(this.SocketAsyncCallBack), appSession).IsSuccess;
			if (isSuccess)
			{
				base.AddClient(appSession);
			}
			else
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteDebug(this.ToString(), string.Format(StringResources.Language.ClientOfflineInfo, endPoint));
				}
			}
		}

		// Token: 0x06000C77 RID: 3191 RVA: 0x000498CC File Offset: 0x00047ACC
		[DebuggerStepThrough]
		private void SocketAsyncCallBack(IAsyncResult ar)
		{
			FujiSPBServer.<SocketAsyncCallBack>d__17 <SocketAsyncCallBack>d__ = new FujiSPBServer.<SocketAsyncCallBack>d__17();
			<SocketAsyncCallBack>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<SocketAsyncCallBack>d__.<>4__this = this;
			<SocketAsyncCallBack>d__.ar = ar;
			<SocketAsyncCallBack>d__.<>1__state = -1;
			<SocketAsyncCallBack>d__.<>t__builder.Start<FujiSPBServer.<SocketAsyncCallBack>d__17>(ref <SocketAsyncCallBack>d__);
		}

		// Token: 0x06000C78 RID: 3192 RVA: 0x0004990C File Offset: 0x00047B0C
		private byte[] CreateResponseBack(byte err, string command, byte[] data, bool addLength = true)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(':');
			stringBuilder.Append(this.Station.ToString("X2"));
			stringBuilder.Append("00");
			stringBuilder.Append(command.Substring(9, 4));
			stringBuilder.Append(err.ToString("X2"));
			bool flag = err == 0 && data != null;
			if (flag)
			{
				if (addLength)
				{
					stringBuilder.Append(FujiSPBOverTcp.AnalysisIntegerAddress(data.Length / 2));
				}
				stringBuilder.Append(data.ToHexString());
			}
			stringBuilder[3] = ((stringBuilder.Length - 5) / 2).ToString("X2")[0];
			stringBuilder[4] = ((stringBuilder.Length - 5) / 2).ToString("X2")[1];
			stringBuilder.Append("\r\n");
			return Encoding.ASCII.GetBytes(stringBuilder.ToString());
		}

		// Token: 0x06000C79 RID: 3193 RVA: 0x00049A14 File Offset: 0x00047C14
		private int AnalysisAddress(string address)
		{
			string value = address.Substring(2) + address.Substring(0, 2);
			return Convert.ToInt32(value);
		}

		// Token: 0x06000C7A RID: 3194 RVA: 0x00049A44 File Offset: 0x00047C44
		private byte[] ReadFromSPBCore(byte[] receive)
		{
			bool flag = receive.Length < 15;
			byte[] result;
			if (flag)
			{
				result = null;
			}
			else
			{
				bool flag2 = receive[receive.Length - 2] == 13 && receive[receive.Length - 1] == 10;
				if (flag2)
				{
					receive = receive.RemoveLast(2);
				}
				string @string = Encoding.ASCII.GetString(receive);
				int num = Convert.ToInt32(@string.Substring(3, 2), 16);
				bool flag3 = num != (@string.Length - 5) / 2;
				if (flag3)
				{
					result = this.CreateResponseBack(3, @string, null, true);
				}
				else
				{
					bool flag4 = @string.Substring(9, 4) == "0000";
					if (flag4)
					{
						result = this.ReadByCommand(@string);
					}
					else
					{
						bool flag5 = @string.Substring(9, 4) == "0100";
						if (flag5)
						{
							result = this.WriteByCommand(@string);
						}
						else
						{
							bool flag6 = @string.Substring(9, 4) == "0102";
							if (flag6)
							{
								result = this.WriteBitByCommand(@string);
							}
							else
							{
								result = null;
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x06000C7B RID: 3195 RVA: 0x00049B3C File Offset: 0x00047D3C
		private byte[] ReadByCommand(string command)
		{
			string a = command.Substring(13, 2);
			int num = this.AnalysisAddress(command.Substring(15, 4));
			int num2 = this.AnalysisAddress(command.Substring(19, 4));
			bool flag = num2 > 105;
			if (flag)
			{
				this.CreateResponseBack(3, command, null, true);
			}
			bool flag2 = a == "0C";
			byte[] result;
			if (flag2)
			{
				result = this.CreateResponseBack(0, command, this.dBuffer.GetBytes(num * 2, num2 * 2), true);
			}
			else
			{
				bool flag3 = a == "0D";
				if (flag3)
				{
					result = this.CreateResponseBack(0, command, this.rBuffer.GetBytes(num * 2, num2 * 2), true);
				}
				else
				{
					bool flag4 = a == "0E";
					if (flag4)
					{
						result = this.CreateResponseBack(0, command, this.wBuffer.GetBytes(num * 2, num2 * 2), true);
					}
					else
					{
						bool flag5 = a == "01";
						if (flag5)
						{
							result = this.CreateResponseBack(0, command, this.xBuffer.GetBytes(num * 2, num2 * 2), true);
						}
						else
						{
							bool flag6 = a == "00";
							if (flag6)
							{
								result = this.CreateResponseBack(0, command, this.yBuffer.GetBytes(num * 2, num2 * 2), true);
							}
							else
							{
								bool flag7 = a == "02";
								if (flag7)
								{
									result = this.CreateResponseBack(0, command, this.mBuffer.GetBytes(num * 2, num2 * 2), true);
								}
								else
								{
									result = this.CreateResponseBack(2, command, null, true);
								}
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x06000C7C RID: 3196 RVA: 0x00049CBC File Offset: 0x00047EBC
		private byte[] WriteByCommand(string command)
		{
			string a = command.Substring(13, 2);
			int num = this.AnalysisAddress(command.Substring(15, 4));
			int num2 = this.AnalysisAddress(command.Substring(19, 4));
			bool flag = num2 * 4 != command.Length - 23;
			byte[] result;
			if (flag)
			{
				result = this.CreateResponseBack(3, command, null, true);
			}
			else
			{
				byte[] data = command.Substring(23).ToHexBytes();
				bool flag2 = a == "0C";
				if (flag2)
				{
					this.dBuffer.SetBytes(data, num * 2);
					result = this.CreateResponseBack(0, command, null, true);
				}
				else
				{
					bool flag3 = a == "0D";
					if (flag3)
					{
						this.rBuffer.SetBytes(data, num * 2);
						result = this.CreateResponseBack(0, command, null, true);
					}
					else
					{
						bool flag4 = a == "0E";
						if (flag4)
						{
							this.wBuffer.SetBytes(data, num * 2);
							result = this.CreateResponseBack(0, command, null, true);
						}
						else
						{
							bool flag5 = a == "00";
							if (flag5)
							{
								this.yBuffer.SetBytes(data, num * 2);
								result = this.CreateResponseBack(0, command, null, true);
							}
							else
							{
								bool flag6 = a == "02";
								if (flag6)
								{
									this.mBuffer.SetBytes(data, num * 2);
									result = this.CreateResponseBack(0, command, null, true);
								}
								else
								{
									result = this.CreateResponseBack(2, command, null, true);
								}
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x06000C7D RID: 3197 RVA: 0x00049E30 File Offset: 0x00048030
		private byte[] WriteBitByCommand(string command)
		{
			string a = command.Substring(13, 2);
			int num = this.AnalysisAddress(command.Substring(15, 4));
			int num2 = Convert.ToInt32(command.Substring(19, 2));
			bool value = command.Substring(21, 2) != "00";
			bool flag = a == "0C";
			byte[] result;
			if (flag)
			{
				this.dBuffer.SetBool(value, num * 8 + num2);
				result = this.CreateResponseBack(0, command, null, true);
			}
			else
			{
				bool flag2 = a == "0D";
				if (flag2)
				{
					this.rBuffer.SetBool(value, num * 8 + num2);
					result = this.CreateResponseBack(0, command, null, true);
				}
				else
				{
					bool flag3 = a == "0E";
					if (flag3)
					{
						this.wBuffer.SetBool(value, num * 8 + num2);
						result = this.CreateResponseBack(0, command, null, true);
					}
					else
					{
						bool flag4 = a == "00";
						if (flag4)
						{
							this.yBuffer.SetBool(value, num * 8 + num2);
							result = this.CreateResponseBack(0, command, null, true);
						}
						else
						{
							bool flag5 = a == "02";
							if (flag5)
							{
								this.mBuffer.SetBool(value, num * 8 + num2);
								result = this.CreateResponseBack(0, command, null, true);
							}
							else
							{
								result = this.CreateResponseBack(2, command, null, true);
							}
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 启动SPB串口的从机服务，使用默认的参数进行初始化串口，9600波特率，8位数据位，无奇偶校验，1位停止位<br />
		/// Start the slave service of modbus-rtu, initialize the serial port with default parameters, 9600 baud rate, 8 data bits, no parity, 1 stop bit
		/// </summary>
		/// <param name="com">串口信息</param>
		// Token: 0x06000C7E RID: 3198 RVA: 0x00049F8B File Offset: 0x0004818B
		public void StartSerial(string com)
		{
			this.StartSerial(com, 9600);
		}

		/// <summary>
		/// 启动SPB串口的从机服务，使用默认的参数进行初始化串口，8位数据位，无奇偶校验，1位停止位<br />
		/// Start the slave service of modbus-rtu, initialize the serial port with default parameters, 8 data bits, no parity, 1 stop bit
		/// </summary>
		/// <param name="com">串口信息</param>
		/// <param name="baudRate">波特率</param>
		// Token: 0x06000C7F RID: 3199 RVA: 0x00049F9C File Offset: 0x0004819C
		public void StartSerial(string com, int baudRate)
		{
			this.StartSerial(delegate(SerialPort sp)
			{
				sp.PortName = com;
				sp.BaudRate = baudRate;
				sp.DataBits = 8;
				sp.Parity = Parity.None;
				sp.StopBits = StopBits.One;
			});
		}

		/// <summary>
		/// 启动SPB串口的从机服务，使用自定义的初始化方法初始化串口的参数<br />
		/// Start the slave service of modbus-rtu and initialize the parameters of the serial port using a custom initialization method
		/// </summary>
		/// <param name="inni">初始化信息的委托</param>
		// Token: 0x06000C80 RID: 3200 RVA: 0x00049FD4 File Offset: 0x000481D4
		public void StartSerial(Action<SerialPort> inni)
		{
			bool flag = !this.serialPort.IsOpen;
			if (flag)
			{
				if (inni != null)
				{
					inni(this.serialPort);
				}
				this.serialPort.ReadBufferSize = 1024;
				this.serialPort.ReceivedBytesThreshold = 1;
				this.serialPort.Open();
				this.serialPort.DataReceived += this.SerialPort_DataReceived;
			}
		}

		/// <summary>
		/// 关闭SPB的串口对象<br />
		/// Close the serial port object of modbus-rtu
		/// </summary>
		// Token: 0x06000C81 RID: 3201 RVA: 0x0004A04C File Offset: 0x0004824C
		public void CloseSerial()
		{
			bool isOpen = this.serialPort.IsOpen;
			if (isOpen)
			{
				this.serialPort.Close();
			}
		}

		/// <summary>
		/// 接收到串口数据的时候触发
		/// </summary>
		/// <param name="sender">串口对象</param>
		/// <param name="e">消息</param>
		// Token: 0x06000C82 RID: 3202 RVA: 0x0004A078 File Offset: 0x00048278
		private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			int num = 0;
			byte[] array = new byte[1024];
			bool flag;
			do
			{
				Thread.Sleep(20);
				int num2 = this.serialPort.Read(array, num, this.serialPort.BytesToRead);
				num += num2;
				flag = (num2 == 0);
			}
			while (!flag);
			bool flag2 = num == 0;
			if (!flag2)
			{
				byte[] array2 = array.SelectBegin(num);
				bool flag3 = array2.Length < 5;
				if (flag3)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteError(this.ToString(), "[" + this.serialPort.PortName + "] Uknown Data：" + array2.ToHexString(' '));
					}
				}
				else
				{
					bool flag4 = array2[0] != 58;
					if (!flag4)
					{
						ILogNet logNet2 = base.LogNet;
						if (logNet2 != null)
						{
							logNet2.WriteDebug(this.ToString(), string.Concat(new string[]
							{
								"[",
								this.serialPort.PortName,
								"] Ascii ",
								StringResources.Language.Receive,
								"：",
								Encoding.ASCII.GetString(array2.RemoveLast(2))
							}));
						}
						bool flag5 = Encoding.ASCII.GetString(array2, 1, 2) != this.station.ToString("X2");
						if (flag5)
						{
							ILogNet logNet3 = base.LogNet;
							if (logNet3 != null)
							{
								logNet3.WriteDebug(this.ToString(), string.Format("[{0}] Station not match , Except: {1:X2} , Actual: {2}", this.serialPort.PortName, this.station, Encoding.ASCII.GetString(array2, 1, 2)));
							}
						}
						else
						{
							byte[] array3 = this.ReadFromSPBCore(array2);
							bool flag6 = array3 == null;
							if (!flag6)
							{
								this.serialPort.Write(array3, 0, array3.Length);
								ILogNet logNet4 = base.LogNet;
								if (logNet4 != null)
								{
									logNet4.WriteDebug(this.ToString(), string.Concat(new string[]
									{
										"[",
										this.serialPort.PortName,
										"] Ascii ",
										StringResources.Language.Send,
										"：",
										Encoding.ASCII.GetString(array3.RemoveLast(2))
									}));
								}
								bool isStarted = base.IsStarted;
								if (isStarted)
								{
									base.RaiseDataReceived(array2);
								}
							}
						}
					}
				}
			}
		}

		/// <inheritdoc />
		// Token: 0x06000C83 RID: 3203 RVA: 0x0004A2CC File Offset: 0x000484CC
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				this.xBuffer.Dispose();
				this.yBuffer.Dispose();
				this.mBuffer.Dispose();
				this.dBuffer.Dispose();
				this.rBuffer.Dispose();
				this.wBuffer.Dispose();
				SerialPort serialPort = this.serialPort;
				if (serialPort != null)
				{
					serialPort.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		/// <inheritdoc />
		// Token: 0x06000C84 RID: 3204 RVA: 0x0004A343 File Offset: 0x00048543
		public override string ToString()
		{
			return string.Format("FujiSPBServer[{0}]", base.Port);
		}

		// Token: 0x04000362 RID: 866
		private SerialPort serialPort;

		// Token: 0x04000363 RID: 867
		private SoftBuffer xBuffer;

		// Token: 0x04000364 RID: 868
		private SoftBuffer yBuffer;

		// Token: 0x04000365 RID: 869
		private SoftBuffer mBuffer;

		// Token: 0x04000366 RID: 870
		private SoftBuffer dBuffer;

		// Token: 0x04000367 RID: 871
		private SoftBuffer rBuffer;

		// Token: 0x04000368 RID: 872
		private SoftBuffer wBuffer;

		// Token: 0x04000369 RID: 873
		private const int DataPoolLength = 65536;

		// Token: 0x0400036A RID: 874
		private int station = 1;
	}
}
